package com.cloudcentral.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudcentral.entity.Radio;
import com.cloudcentral.entity.query.RadioQuery;
import com.cloudcentral.entity.vo.RadioDetailVO;
import com.cloudcentral.entity.vo.RadioVO;
import com.cloudcentral.enums.RadioStatus;
import com.cloudcentral.exception.FailureException;
import com.cloudcentral.mapper.RadioDeviceMapper;
import com.cloudcentral.mapper.RadioMapper;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 台站信息表 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Slf4j
@Service
public class RadioService extends ServiceImpl<RadioMapper, Radio> {
  private final RadioDeviceMapper radioDeviceMapper;

  @Autowired
  public RadioService(RadioDeviceMapper radioDeviceMapper) {
    this.radioDeviceMapper = radioDeviceMapper;
  }

  @Transactional(rollbackFor = {Exception.class})
  @GlobalTransactional(rollbackFor = {Exception.class})
  public Radio register(Radio radio) throws FailureException {
    if (radio.getId() == null) throw new FailureException("缺少台站ID");
    else if (StringUtils.isBlank(radio.getFpgaVersion())) throw new FailureException("缺少FPGA版本");
    else if (StringUtils.isBlank(radio.getFpServerVersion())) throw new FailureException("缺少FPServer版本");
    else {
      if (StringUtils.isBlank(radio.getName())) {
        Radio origin = getById(radio.getId());
        radio.setName(origin == null ? "未命名-" + radio.getId() : origin.getName());
      }
      saveOrUpdate(radio);
      return getById(radio.getId());
    }
  }

  @Transactional(rollbackFor = {Exception.class})
  @GlobalTransactional(rollbackFor = {Exception.class})
  public Radio submit(Radio radio) throws FailureException {
    if (radio.getId() == null) throw new FailureException("缺少台站ID");
    else if (StringUtils.isBlank(radio.getName())) throw new FailureException("缺少名称");
    else if (StringUtils.isBlank(radio.getFpgaVersion())) throw new FailureException("缺少FPGA版本");
    else if (StringUtils.isBlank(radio.getFpServerVersion())) throw new FailureException("缺少FPServer版本");
    else {
      saveOrUpdate(radio);
      return getById(radio.getId());
    }
  }

  @Transactional(rollbackFor = {Exception.class})
  @GlobalTransactional(rollbackFor = {Exception.class})
  public Boolean delete(Integer id) {
    return removeById(id);
  }

  public RadioDetailVO select(Integer id) throws Exception {
    RadioDetailVO radioDetailVO = baseMapper.selectDetail(id);
    radioDetailVO.setDevice(radioDeviceMapper.selectDeviceInfo(id));
    radioDetailVO.setStatus(RadioStatus.value(id).name());
    return radioDetailVO;
  }

  public List<RadioVO> listAll(RadioQuery query) {
    List<RadioVO> list = baseMapper.listAll(query);
    return list.stream().peek(item -> item.setStatus(RadioStatus.value(Integer.valueOf(item.getId())).name())).collect(Collectors.toList());
  }
}
